package com.longtop.leaning.serializable;

import java.io.IOException;

import com.longtop.leaning.util.Base64;



public abstract class BaseSerializable implements ISerializable {

	/*
	 * 进行反编码并返回解析后的对像实例(non-Javadoc)
	 * @see com.onest.xmair.serializable.ISerializable#decode(java.lang.String)
	 * Create by haicao
	 * 2009-7-5
	 */
	public Object decode(String code) throws IOException {
		byte[] decode = null;
		decode = Base64.decode(code);
		return deserialize(decode);
	}
	
	/*
	 * 进行编码并返回编码后的BASE64字串(non-Javadoc)
	 * @see com.onest.xmair.serializable.ISerializable#encode(java.lang.Object)
	 * Create by haicao
	 * 2009-7-5
	 */
	public String encode(Object obj) {
		byte[] byteResult = serialize(obj);
		String code = Base64.encode(byteResult);
		return code;
	}

	/*
	 * 进行对像系列化并返回byte数组(non-Javadoc)
	 * 本方法由具体序列化服务类实现
	 * @see com.onest.xmair.serializable.ISerializable#serialize(java.lang.Object)
	 * Create by haicao
	 * 2009-7-5
	 */
	public abstract byte[] serialize(Object obj);
	
	/*
	 * 对byte数组进行反系列化并返回对像(non-Javadoc)
	 * @see com.onest.xmair.serializable.ISerializable#deserialize(byte[])
	 * Create by haicao
	 * 2009-7-5
	 */
	public abstract Object deserialize(byte[] data);
	
	/*
	 * 检查对像所有属性值，确保不出现属性为null的情况(non-Javadoc)
	 * @see com.onest.xmair.serializable.ISerializable#checkObjValue(Object)
	 * Create by haicao
	 * 2009-7-6
	 */
	public abstract void checkObjValue(Object obj);
}
